#include "mainwidget.h"
#include "ui_mainwidget.h"

#include "purchaseorderdialog.h"
#include "purchaseorderlistdialog.h"
#include "receivingformdialog.h"
#include "stockindialog.h"
#include "stockviewwidget.h"
MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , db(QSqlDatabase::addDatabase("QMYSQL"))
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    //配置数据库
    db.setHostName("172.17.0.2");
    db.setPort(3306);
    db.setDatabaseName("imis");
    db.setUserName("root");
    db.setPassword("0878");

    //连接测试
    if(db.open())
    {
        QMessageBox::information(this,"连接提示","连接成功");
    }
    else
    {
        QMessageBox::warning(this,"连接提示","连接失败");
        //使用定时器延迟关闭，避免消息框阻塞
        QTimer::singleShot(0, this, &MainWidget::close);
    }
}

MainWidget::~MainWidget()
{
    if (db.isOpen()) {
        db.close(); // 确保数据库连接在窗口关闭时关闭
    }
    delete ui;
}

void MainWidget::on_pbPurchaseOrder_clicked()
{
    //打开创建采购订单对话框
    this->hide();
    PurchaseOrderDialog *poDialog = new PurchaseOrderDialog(this);  //堆空间创建对象
    poDialog->setAttribute(Qt::WA_DeleteOnClose);//关闭时自动删除对象
    poDialog->show();
    //连接对话框关闭信号到主窗口的显示槽
    connect(poDialog,&PurchaseOrderDialog::poDialogClosed,this,&MainWidget::showMainWindow);
}
void MainWidget::showMainWindow()
{
    //显示主窗口
    this->show();
}



void MainWidget::on_pbPurchaseOrderList_clicked()
{
    //打开采购订单列表对话框
    this->hide();
    PurchaseOrderListDialog *poListDialog = new PurchaseOrderListDialog(this);  //堆空间创建对象
    poListDialog->setAttribute(Qt::WA_DeleteOnClose);//关闭时自动删除对象
    poListDialog->show();
    //连接对话框关闭信号到主窗口的显示槽
    connect(poListDialog,&PurchaseOrderListDialog::poListDialogClosed,this,&MainWidget::showMainWindow);
}


void MainWidget::on_pbReceivingForm_clicked()
{
    //打开创建验收单页面
    this->hide();
    ReceivingFormDialog *reportDialog = new ReceivingFormDialog(this);
    reportDialog->setAttribute(Qt::WA_DeleteOnClose);
    reportDialog->show();
    connect(reportDialog, &ReceivingFormDialog::reportDialogClosed, this, &MainWidget::showMainWindow);

}


void MainWidget::on_pbStockIn_clicked()
{
    //打开创建入库单页面
    this->hide();
    StockInDialog *stockInDialog = new StockInDialog(this);
    stockInDialog->setAttribute(Qt::WA_DeleteOnClose);
    stockInDialog->show();
    connect(stockInDialog, &StockInDialog::stockInDialogClosed, this, &MainWidget::showMainWindow);

}


void MainWidget::on_pbStock_clicked()
{
    this->hide();
    StockViewWidget *stockWidget = new StockViewWidget();
    stockWidget->setAttribute(Qt::WA_DeleteOnClose);
    stockWidget->show();
    connect(stockWidget, &StockViewWidget::destroyed, this, &MainWidget::showMainWindow);
}

